//
//  Collection.m
//  UI_Project_A
//
//  Created by GH on 16/3/3.
//  Copyright © 2016年 GH. All rights reserved.
//

#import "Collection.h"
#import "CollectionTableViewCell.h"
#import "ModelOfInternet.h"
#import <UIImageView+WebCache.h>
#import "ContectViewcontroller.h"

@interface Collection ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,retain)UITableView *tableview;
@property (nonatomic ,retain)NSMutableArray *arrayData;

@end

@implementation Collection

- (void) dealloc {

    [_tableview release];
    [super dealloc];
}


- (void)viewWillAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    [self unArcher];
    if (self.arrayData.count == 0) {
        
        UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zanwu"]];
        backgroundImage.frame = CGRectMake(0, 80, WIDTH, HEIGHT - 80);
        [self.view addSubview:backgroundImage];
        [backgroundImage release];
        
        
    }else{
        
        [self.tableview reloadData];
    }
}



- (void)viewDidLoad {
    [super viewDidLoad];

    [self unArcher];
    self.view.backgroundColor = [UIColor whiteColor];
    if (self.arrayData.count == 0) {
        
        UIImageView *backgroundImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"zanwu"]];
        backgroundImage.frame = CGRectMake(0, 80, WIDTH, HEIGHT - 80);
        [self.view addSubview:backgroundImage];
        [backgroundImage release];
        
        
    }else{
    
    [self createTable];
        
    }
    [self createView];
    
}
-(void)unArcher {
    
    NSString *file = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES)lastObject]stringByAppendingPathComponent:@"arr.da"];
    
    NSData *datcome = [NSData dataWithContentsOfFile:file];
    
    //反归档
    NSKeyedUnarchiver *unarcher =[[NSKeyedUnarchiver alloc] initForReadingWithData:datcome];
    self.arrayData = [unarcher decodeObjectForKey:@"model"];
    [unarcher finishDecoding];
    [unarcher release];
    
 

}


- (void)createView {

    UIView *viewOfback = [[UIView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 80)];
    [self.view addSubview:viewOfback];
    viewOfback.backgroundColor = [UIColor redColor];

    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH/2 - 50, 20, 100, 50)];
    [viewOfback addSubview:label];
    [label release];
    
    label.text = @"收藏";
    label.font = [UIFont systemFontOfSize:25];
    label.textAlignment = 1;
    label.textColor = [UIColor whiteColor];
    
    
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [viewOfback addSubview:button];
    [button release];
    button.frame = CGRectMake(20, 20, 50, 50);
    [button setImage:[UIImage imageNamed:@"jiantou3"] forState:UIControlStateNormal];

    [button addTarget:self action:@selector(returnButton:) forControlEvents:UIControlEventTouchUpInside];

}

- (void)returnButton:(UIButton *)button {
    
    [self dismissViewControllerAnimated:YES completion:nil];

}



- (void)createTable {
  

    
    

    self.tableview = [[UITableView alloc] initWithFrame:CGRectMake(0, 80, WIDTH,HEIGHT - 80) style:UITableViewStylePlain];
    [self.view addSubview:self.tableview];
    [self.tableview release];
    self.tableview.dataSource = self;
    self.tableview.delegate = self;
    
    
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerClass:[CollectionTableViewCell class] forCellReuseIdentifier:@"pool"];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrayData.count;
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {

    CollectionTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"pool"];

    
  
    
        ModelOfInternet *model = [self.arrayData objectAtIndex:indexPath.row];
        
        cell.label.text = model.title;
        
        NSString *url = [self Substring:model.url];
        
        
        [cell.imageviewInt sd_setImageWithURL:[NSURL URLWithString:url]];
        

    NSString *modelTime = [model.ts substringToIndex:10];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"HH:mm"];
    [dateFormatter setDateFormat:@"MM-dd"];
    [dateFormatter release];
    
    
    NSDate *now = [NSDate date];
    NSTimeInterval time = [now timeIntervalSince1970];
    float seconds = (int)time - modelTime.doubleValue;
    float points = seconds / 60;
    if (points < 60) {
        cell.labelTime.text = [NSString stringWithFormat:@"%d分钟前", (int)points];
    }else if (points > 60 && points < 3600) {
        
        cell.labelTime.text = [NSString stringWithFormat:@"1小时前"];
    }else {
        cell.labelTime.text = [NSString stringWithFormat:@"2小时前"];
    }
    

    
    
    
    

    return cell;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return 150;
}






- (NSString *)Substring:(NSString *)urlString {
    
    NSString *sub = urlString;
    
    if ([sub hasSuffix:@"jpg"]) {
        return urlString;
    }else {
        NSRange range = [urlString rangeOfString:@"u="];
        NSString *sub1 = [urlString substringFromIndex:range.location+2];
        
        NSRange lastRange = [sub1 rangeOfString:@"&"];
        sub = [sub1 substringToIndex:lastRange.location];
        
    }
    
    
    
    return sub;
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    ModelOfInternet *model = [self.arrayData objectAtIndex:indexPath.row];
    
    ContectViewcontroller *contect = [[ContectViewcontroller alloc] init];
    
    contect.model = model;
    [self presentViewController:contect animated:YES completion:^{
        
    }];

    
    

}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
